/* exponencial smoothing */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define dbg printf
static char filter_sntp_b=0,filter_sntp_i=0;
static int myfilter(int *off,int d,char *b,char *i)
{
#define RANK_B 3	
#define RANK_I 3	
	int ret = 0;
	int absoff ;
	absoff = abs(*off);
	if(*b < RANK_B){
		if(*i < RANK_I){ 			
			if(absoff < d)
				(*i)++;	
			else if((*i)>0)
				(*i)--;
		}
		else {					
			if(absoff<(d)){
				if(*b>0){
					(*b)--;
				}
			}
			else {
				(*b)++; 		
				*off = 0; 		
			}
			ret=1;
		}
	}
	else{  						
		*i = 0;
		*b = 0;
	}
	dbg("off=%d, *i=%d, *b=%d\n",*off,*i,*b);
	return ret;
}
#define ADJUST_OFF 500 //ms
#define NSATR_SNTP_FILTER(off)	myfilter(off, ADJUST_OFF, &filter_sntp_b, &filter_sntp_i)

int buf1[200] ;

int buf2[200] ;

int main(void) {
    int input = 0, ra = 0, add = 0;
    int b = 0;

    srand(time(NULL));
    int i = 0;
    for(i = 30; i < 130; i++) {
#if 0
        if(i > 70) {
            b = i/5 + 100;
        }else {
            b = i/5 ;

        }

#else 
    
     if(i > 80) {
            b = 2150 + i*12 ;
        }else {
            b = 3150 + i*16 ;

        }

      if(i ==  70) {
            b -= 3800;
        }else if(i ==  90){
            b -= 1200;

        }
#endif
        add = rand()%100 + b;
        ra= i + add;
        int off = ra - input ;
        NSATR_SNTP_FILTER(&off);
        input = input + off;
        //dbg("%d / %d\n", ra, input);
        buf1[i] = ra;
        buf2[i] = input;
    }



    for(i = 30; i < 130; i++) {
        dbg("%d.%d\n", buf1[i]/100, buf1[i]%100);
    }
    dbg("\n\n");
     for(i = 30; i < 130; i++) {
        dbg("%d.%d\n", buf2[i]/100, buf2[i]%100);
    }
}


